package com.demo.widget.guhong;

import com.demo.widget.BaseAbility;
import com.demo.widget.ResourceTable;

import com.meis.widget.ball.BounceBallView;

import cs.help.tool.Log;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;

/**
 * desc:
 * author: wens
 * date: 2018/4/29.
 */
public class BounceBallAbility extends BaseAbility {
    private BounceBallView bbv1;
    private TextField bounceCount;
    private TextField ballCount;
    private TextField ballDelay;
    private TextField duration;
    private TextField radius;
    private Checkbox physicMode;
    private Checkbox randomPath;
    private Checkbox randomColor;
    private Checkbox randomRadius;
    private Button b1;
    private Button b2;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_bounce_ball_ability);
        bbv1 = (BounceBallView) findComponentById(ResourceTable.Id_bbv1);
        ballCount = (TextField) findComponentById(ResourceTable.Id_ball_count);
        ballDelay = (TextField) findComponentById(ResourceTable.Id_ball_delay);
        bounceCount = (TextField) findComponentById(ResourceTable.Id_bounce_count);
        radius = (TextField) findComponentById(ResourceTable.Id_radius);
        duration = (TextField) findComponentById(ResourceTable.Id_duration);
        physicMode = (Checkbox) findComponentById(ResourceTable.Id_physic_mode);
        randomColor = (Checkbox) findComponentById(ResourceTable.Id_random_color);
        randomPath = (Checkbox) findComponentById(ResourceTable.Id_random_path);
        randomRadius = (Checkbox) findComponentById(ResourceTable.Id_random_radius);
        initText();
        bbv1.start();
        b2 = (Button) findComponentById(ResourceTable.Id_b2);
        b2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                apply(bbv1);
                bbv1.start();
                initText();
            }
        });
        b1 = (Button) findComponentById(ResourceTable.Id_b1);
        b1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                CommonDialog commonDialog = new CommonDialog(BounceBallAbility.this);
                Component component1 = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_bounce_ball_dialog,   null, false);
                component1.setLayoutConfig(
                        new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                                DirectionalLayout.LayoutConfig.MATCH_PARENT));
                commonDialog.setSize(750, 450);
                BounceBallView bbv = (BounceBallView) component1.findComponentById(ResourceTable.Id_ball_view);
                commonDialog.setContentCustomComponent(component1);
                bbv.start();
                commonDialog.setAutoClosable(true);
                commonDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {
                    @Override
                    public void onDestroy() {
                        bbv.cancel();
                    }
                });
                commonDialog.show();
            }
        });
    }

    private void initText() {
        ballCount.setText(bbv1.getBallCount() + "");
        ballDelay.setText(bbv1.getBallDelay() + "");
        bounceCount.setText(bbv1.getBounceCount() + "");
        duration.setText(bbv1.getDefaultDuration() + "");
        radius.setText(bbv1.getRadius() + "");
        physicMode.setChecked(bbv1.isPhysicsMode());
        randomRadius.setChecked(bbv1.isRandomRadius());
        randomPath.setChecked(bbv1.isRandomBallPath());
        randomColor.setChecked(bbv1.isRandomColor());
    }

    private void apply(BounceBallView bbv) {
        if (bbv == null) {
            Log.i("BounceBallView is null");
            return;
        }
        try {
            bbv.config()
                .ballCount(Integer.parseInt(ballCount.getText()))
                .bounceCount(Integer.parseInt(bounceCount.getText()))
                .ballDelay(Integer.parseInt(ballDelay.getText()))
                .duration(Integer.parseInt(duration.getText()))
                .radius(Float.parseFloat(radius.getText()))
                .isPhysicMode(physicMode.isChecked())
                .isRamdomPath(randomPath.isChecked())
                .isRandomColor(randomColor.isChecked())
                .isRandomRadius(randomRadius.isChecked())
                .apply();
        } catch (Exception e) {
            Log.i("错误"+e.getMessage());
        }
    }
}
